﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using SuperFox.SuperServiceReference;

namespace SuperFox
{
    public partial class ClientLoginControl : UserControl
    {
        private int CurrentSaveUserIndex { get; set; }


        private SuperServiceClient client;

        public ObservableCollection<DbUsers> Users
        {
            set
            {
                this.UsersGrid.ItemsSource = value;
            }

            get
            {
                if (this.UsersGrid.ItemsSource == null)
                {
                    return null;
                }
                else
                {
                    return (this.UsersGrid.ItemsSource as ObservableCollection<DbUsers>);
                }
            }
        }

        public ClientLoginControl()
        {
            InitializeComponent();

            client = ServiceConfig.Connect();
            client.SaveUserCompleted += new EventHandler<SaveUserCompletedEventArgs>(client_SaveUserCompleted);
            client.DeleteUserCompleted += new EventHandler<DeleteUserCompletedEventArgs>(client_DeleteUserCompleted);
        }

        void client_SaveUserCompleted(object sender, SaveUserCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                MessageBox.Show("Error Saving Users. Please Logout and try again.");
                return;
            }

            if (e.Result == -1)
            {
                MessageBox.Show("Error Saving Users. Please Logout and try again.");
                return;
            }


            this.Users[CurrentSaveUserIndex].log_id = e.Result;
            this.Users[CurrentSaveUserIndex].IsNew = false;
            this.Users[CurrentSaveUserIndex].WriteStatus = Config.WriteStatusValues.Saved;

            CurrentSaveUserIndex++;

            if (CurrentSaveUserIndex == this.Users.Count)
            {
                return;
            }



            while (this.Users[CurrentSaveUserIndex].WriteStatus != Config.WriteStatusValues.Changed)
            {
                CurrentSaveUserIndex++;
                if (CurrentSaveUserIndex == this.Users.Count)
                    return;

                
            }


            this.Users[CurrentSaveUserIndex].WriteStatus = Config.WriteStatusValues.Saving;
            client.SaveUserAsync(this.Users[CurrentSaveUserIndex]);
        }

        void client_DeleteUserCompleted(object sender, DeleteUserCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                MessageBox.Show("There was an error deleting this user");
                return;
            }

            if (e.Result == -1)
            {
                MessageBox.Show("There was an error deleting this user");
                return;
            }

            foreach (DbUsers u in this.Users)
            {
                if (u.log_id == e.Result)
                {
                    this.Users.Remove(u);
                    return;
                }
            }


        }

        private void DeleteButton_Click(object sender, RoutedEventArgs e)
        {
            if(MessageBox.Show("Are you sure you want to delete this user?","Confirm Delete",MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                client.DeleteUserAsync(((sender as Button).DataContext as DbUsers).log_id);
            }
                  
        }

        public void Save()
        {
            this.CurrentSaveUserIndex = 0;

            while(this.Users[CurrentSaveUserIndex].WriteStatus != Config.WriteStatusValues.Changed)
            {
                this.CurrentSaveUserIndex++;
                if (CurrentSaveUserIndex == this.Users.Count)
                    return;
            }

            if (this.Users[CurrentSaveUserIndex].IsNew)
            {
                this.Users[CurrentSaveUserIndex].Programs = null;
            }

            this.Users[CurrentSaveUserIndex].WriteStatus = Config.WriteStatusValues.Saving;
            client.SaveUserAsync(this.Users[CurrentSaveUserIndex]);


        }

        //void client_SaveUsersCompleted(object sender, SaveUsersCompletedEventArgs e)
        //{
        //    if (e.Error != null)
        //    {
        //        MessageBox.Show("There was an error saving the user logins");
        //    }

        //    // make sure all the users are no longer new

        //    foreach (DbUsers u in this.Users)
        //    {
        //        u.IsNew = false;
        //    }
        //}


        private void AddButton_Click(object sender, RoutedEventArgs e)
        {
            this.Users.Add(new DbUsers() 
            { 
                IsNew = true,
                WriteStatus = Config.WriteStatusValues.Changed,
                merc_id = (App.Current.RootVisual as MainPage).CurrentUser.merc_id,
                Programs = (App.Current.RootVisual as MainPage).CurrentUser.Programs
            });
        }

        private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            (sender as ComboBox).SelectedIndex = -1;
        }
    }
}
